package com.cloud.elm.controller;

import com.alibaba.fastjson2.JSONObject;
import com.cloud.elm.po.DeliveryAddress;
import com.cloud.elm.service.DeliveryAddressService;
import com.cloud.elm.utils.response.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;


@RestController
@RequestMapping("/elm/DeliveryAddressController")
public class DeliveryAddressController {

	@Autowired
	private DeliveryAddressService deliveryAddressService;
	
	@GetMapping("/listDeliveryAddressByUserId/{userId}")
	public ResultBody<List<DeliveryAddress>> listDeliveryAddressByUserId(@PathVariable("userId") String userId) {
		List<DeliveryAddress> res = deliveryAddressService.listDeliveryAddressByUserId(userId);
		return ResultBody.success(res);
	} 
	
	@GetMapping("/getDeliveryAddressById/{daId}")
	public ResultBody<DeliveryAddress> getDeliveryAddressById(@PathVariable("daId") Integer daId) {
		DeliveryAddress da = deliveryAddressService.getDeliveryAddressById(daId);
		return ResultBody.success(da);
	} 
	
	@PostMapping("/saveDeliveryAddress/{contactName}/{contactSex}/{contactTel}/{address}/{userId}")
	public ResultBody<Integer> saveDeliveryAddress(@PathVariable("contactName") String contactName,
												   @PathVariable("contactSex") Integer contactSex,
												   @PathVariable("contactTel") String contactTel,
												   @PathVariable("address") String address,
												   @PathVariable("userId") String userId) {
		DeliveryAddress da = new DeliveryAddress();
		da.setContactName(contactName);
		da.setContactSex(contactSex);
		da.setContactTel(contactTel);
		da.setAddress(address);
		da.setUserId(userId);
		return ResultBody.success(deliveryAddressService.saveDeliveryAddress(da));
	} 
	
	@PutMapping("/updateDeliveryAddress/{contactName}/{contactSex}/{contactTel}/{address}/{daId}")
	public ResultBody<Integer> updateDeliveryAddress(@PathVariable("contactName") String contactName,
													 @PathVariable("contactSex") Integer contactSex,
													 @PathVariable("contactTel") String contactTel,
													 @PathVariable("address") String address,
													 @PathVariable("daId") Integer daId) {
		DeliveryAddress da = new DeliveryAddress();
		da.setContactName(contactName);
		da.setContactSex(contactSex);
		da.setContactTel(contactTel);
		da.setAddress(address);
		da.setDaId(daId);
		return ResultBody.success(deliveryAddressService.updateDeliveryAddress(da));
	} 
	
	@DeleteMapping("/removeDeliveryAddress/{daId}")
	public ResultBody<Integer> removeDeliveryAddress(@PathVariable("daId") Integer daId) {
		try {
			deliveryAddressService.removeDeliveryAddress(daId);
			return ResultBody.success();
		} catch (Exception e) {
			return ResultBody.error("删除失败", null);
		}
	}

	@GetMapping("/getLastAddr/{userId}")
	public ResultBody<JSONObject> getLastAddr(@PathVariable("userId") String userId) {
		JSONObject jo = new JSONObject();
		Integer daId = deliveryAddressService.getLastAddr(userId);
		DeliveryAddress da = deliveryAddressService.getDeliveryAddressById(daId);
		jo.put("daId", daId);
		jo.put("address", da.getAddress());
		jo.put("contactName", da.getContactName());
		jo.put("contactTel", da.getContactTel());
		jo.put("contactSex", da.getContactSex());
		return ResultBody.success(jo);
	}

	@PostMapping("/setLastAddr/{userId}/{daId}")
	public ResultBody<Integer> setLastAddr(@PathVariable("userId") String userId,
										   @PathVariable("daId") Integer daId) {
		return ResultBody.success(deliveryAddressService.setLastAddr(userId, daId));
	}
}
